package com.kichina.parking.module.task;

import com.kichina.parking.controller.parking.handler.YardStatusInfoHandler;
import com.kichina.parking.module.feign.bean.DeviceLineModel;
import com.kichina.parking.module.feign.deal.CarBanRemoteCall;
import com.kichina.parking.service.logic.bean.YardStatusInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @author Surging
 * @version 1.0
 * @ClassName SyncYardStatus
 * @date 2022-10-09 17:24
 * @desc 同步车场在线状态(一分钟同步一次)
 */
@Configuration
@EnableScheduling
@Slf4j
public class SyncYardStatusTask {

    private final YardStatusInfoHandler yardStatusInfoHandler;
    private final CarBanRemoteCall carBanRemoteCall;

    public SyncYardStatusTask(YardStatusInfoHandler yardStatusInfoHandler, CarBanRemoteCall carBanRemoteCall) {
        this.yardStatusInfoHandler = yardStatusInfoHandler;
        this.carBanRemoteCall = carBanRemoteCall;
    }

    @Scheduled(fixedRate = 1000 * 60)
    public void synStatus() {
        List<YardStatusInfo> yardStatusInfos = yardStatusInfoHandler.getYardStatusInfoList();
        if (CollectionUtils.isNotEmpty(yardStatusInfos)){
            for (YardStatusInfo yardStatusInfo : yardStatusInfos) {
                DeviceLineModel deviceLineModel = new DeviceLineModel();
                deviceLineModel.setIsOnline(true);
                deviceLineModel.setHouseId(yardStatusInfo.getYardPassword());
                deviceLineModel.setCode(yardStatusInfo.getYardKey());
                carBanRemoteCall.cardBanRemotePost(deviceLineModel);
            }
        }
    }
}
